package cn.kgc.web;

import cn.kgc.service.LoginService;
import cn.kgc.service.impl.LoginServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(urlPatterns = "/login")
public class LoginAllServlet extends BaseServlet {
    public String Login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String psw = request.getParameter("psw");
        String roles = request.getParameter("role");
        LoginService loginService=new LoginServiceImpl();
        boolean flag;
        //        学生
        if (roles.equals("sex1")){
            flag = loginService.studentLogin(name, psw);
            if (flag==true){
                HttpSession session = request.getSession();
                session.setAttribute("roles",roles);
                return request.getContextPath()+"/stu-home.jsp";
            }else {
                return request.getContextPath()+"/login.jsp";
            }
            //        教师
        }else if (roles.equals("sex2")){
            flag=loginService.teacherLogin(name,psw);
            if (flag==true){
                HttpSession session = request.getSession();
                session.setAttribute("roles",roles);
                return request.getContextPath()+"/tea-home.jsp";
            }else {
                return request.getContextPath()+"/login.jsp";
            }
            //        管理员
        }else if (roles.equals("sex3")){
            flag=loginService.adminLogin(name,psw);
            if (flag==true){
                HttpSession session = request.getSession();
                session.setAttribute("roles",roles);
                return request.getContextPath()+"/adm-home.jsp";
            }else {
                return request.getContextPath()+"/login.jsp";
            }
        }

        return "login.jsp";
    }
}
